package com.lanou3g.code0418.wait;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/18;
 * Otherwise, I don't know who created it either
 */

/*
wait() 和 sleep() 的区别
共同点是: 都会让线程停止
区别1: wait()是object类的方法,任何对象都有,
    而sleep是Thread类的方法
区别2: wait()必须写在synchronized代码块中,并且必须是锁对象调用
        wait()方法,而sleep没有限制(不需要synchronized)
区别3: wait()会释放锁对象,而sleep()不会释放锁
区别4: wait()可以被别的线程唤醒,而sleep()需要等待时间结束
 */

public class Main {
    public static void main(String[] args) {
        final Object lock = new Object();


        Thread a = new Thread() {
            @Override
            public void run() {
                System.out.println("a线程开始了");
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("a线程结束了");
            }
        };

        a.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("准备唤醒a线程");


        synchronized (lock) {
            lock.notifyAll();
        }

    }

}
